package com.iptv.grape.jss;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iptv.grape.R;
import com.iptv.grape.jss.impls.ImplFactory;
import com.iptv.grape.jss.impls.PlayPro;
import com.iptv.grape.jss.impls.listener.PlayEventListener;
import com.iptv.grape.ui.BaseFrgmt;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;


public class WebFrgmt2 extends BaseFrgmt implements View.OnFocusChangeListener,SeekBar.OnSeekBarChangeListener
, PlayPro {


    // Activity
    private SeekBar seekbar;
    private TextView time_left;
    private TextView time_right;
    private TextView timing;
    @Override
    protected int layoutId() {
        return R.layout.vod_fragment;
    }
    private int duration = 0;
    private int currentPosition;

    @Override
    protected void initView(@NonNull View root, @Nullable Bundle savedInstanceState) {
        time_left = root.findViewById(R.id.time_left);
        time_right = root.findViewById(R.id.time_right);
        timing = root.findViewById(R.id.timing);
        seekbar = root.findViewById(R.id.seekbar);
        seekbar.setTag("seekbar");
        seekbar.setOnSeekBarChangeListener(this);
        seekbar.setOnFocusChangeListener(this);
        seekbar.setKeyProgressIncrement(1000 * 20);
        if(duration!=0) {
            seekbar.setMax(duration);
            time_right.setText(CommUtil.getShowTimeStringByEnd(duration));
            seekbar.setProgress(currentPosition);
            time_left.setText(CommUtil.getShowTimeStringByEnd(currentPosition));
        }

        seekbar.requestFocus();

    }

    // 销毁
    public void onDestroy() {
        super.onDestroy();
    }

    // 界面隐藏显示回调函数
    @Override
    public void onHiddenChanged(boolean hidden){
        super.onHiddenChanged(hidden);
        // 不在最前端界面显示
        if (hidden) {
            // 隐藏
        } else {
            this.time_right.setText(CommUtil.getShowTimeStringByEnd(duration));
            seekbar.setMax(duration);
            seekbar.setKeyProgressIncrement(1000 * 20);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //rel_horitation.reSetFocus();
                    seekbar.requestFocus();

                }
            }, 100);
        }
    }

    public static WebFrgmt2 newInstance() {
        WebFrgmt2 frag = new WebFrgmt2();
        Bundle bundle = new Bundle();
        //bundle.putString("dateTimeString", dateTimeString);
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

    @Override
    public synchronized void onProgressChanged(SeekBar seekBar, int progressVal, boolean isUserSet) {

        // 进度条游标(HH:MM:SS)
        //timing.setText(CommUtil.formatDate(selectBill.getBillStartDate(),"HH:mm:ss"));
        int quotaWidth = timing.getWidth();
        final Drawable thumb = seekBar.getThumb();
        //获取thumb的位置
        final Rect bounds = thumb.getBounds();
        //curStartDate.setTime(curStartDate.getTime() + i);
        timing.setText(CommUtil.getShowTime(progressVal));
        timing.setX((thumb.getIntrinsicWidth() - quotaWidth) / 2
                + bounds.left + seekBar.getX());

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        ImplFactory.istc().setProCallStart();
    }

    @Override
    public synchronized void onStopTrackingTouch(SeekBar seekBar) {
     ImplFactory.istc().setProCall(seekBar.getProgress());
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            timing.setBackgroundResource(R.drawable.timing_fucs);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.white));
        }else {
            timing.setBackgroundResource(R.drawable.timing_blus);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.item_bill4));
        }
    }
    private PlayEventListener mlistener=null;
    public void initPro(int size, int pro) {
         duration = size;
         currentPosition = pro;
        if(seekbar!=null) {
            seekbar.setMax(duration);
            time_right.setText(CommUtil.getShowTimeStringByEnd(duration));
            seekbar.setProgress(currentPosition);
            time_left.setText(CommUtil.getShowTimeStringByEnd(currentPosition));
        }
    }
    public void setListener( PlayEventListener listener){
        listener.addProListener("wf2Listenser",this);
    }

    @Override
    public void pro(int pro) {
        AppLog.getCurLog().e("进入  fasdfasfdasd");
        currentPosition = pro;
        if(seekbar!=null) {
            seekbar.setProgress(currentPosition);
            time_left.setText(CommUtil.getShowTimeStringByEnd(currentPosition));
        }
    }

    @Override
    public void completion() {

    }
}

